﻿using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;

namespace SQLDataTransferer.ViewModel
{
    public abstract class BaseViewModel : INotifyPropertyChanged, IDisposable
    {
        #region Fields

        RelayCommand _closeCommand;

        #endregion // Fields

        #region Constructor

        protected BaseViewModel()
        {
        }

        #endregion // Constructor

        #region Properties

        public virtual System.Globalization.CultureInfo CurrentCulture { get; protected set; }

        public System.Windows.Threading.Dispatcher DispatcherObject { get; set; }

        public System.Windows.Window MainWindow { get; set; }

        public virtual string DisplayName { get; protected set; }

        #endregion

        #region CloseCommand

        /// <summary>
        /// Returns the command that, when invoked, attempts
        /// to remove this workspace from the user interface.
        /// </summary>
        public ICommand CloseCommand
        {
            get
            {
                if (_closeCommand == null)
                    _closeCommand = new RelayCommand(param => this.OnRequestClose());

                return _closeCommand;
            }
        }

        #endregion // CloseCommand

        #region Methods

        protected void SetFocus() 
        {
            FocusManager.SetFocusedElement(this.MainWindow, this.MainWindow);
        }

        protected void RunTaskDialog(string dialogText, Action action) 
        {
            Common.Dialogs.ProgressDialog dlg = new Common.Dialogs.ProgressDialog()
            {
                Owner = this.MainWindow,
                DialogText = dialogText,
            };
            dlg.RunTask(action);
        }

        #endregion

        #region RequestClose [event]

        /// <summary>
        /// Raised when this workspace should be removed from the UI.
        /// </summary>
        public event EventHandler RequestClose;

        void OnRequestClose()
        {
            if (this.RequestClose != null)
                this.RequestClose(this, EventArgs.Empty);
        }

        #endregion // RequestClose [event]

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion

        #region IDisposable Members

        /// <summary>
        /// Calls the OnDispose method
        /// </summary>
        public void Dispose()
        {
            this.OnDispose();
        }

        /// <summary>
        /// Override to do a custom cleanup
        /// </summary>
        protected virtual void OnDispose() { }

        #endregion
    }
}
